from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtNetwork import *
from ui.Ui_mainBody import Ui_MainWindow
import sys
import json
import requests
import service.audio_control_service as ac
import service.weather_service as weather
import service.music_service as ms


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.__web__init()
        self.show()
        self.__weather__init()
        # 刷新日期
        self.set_time()
        self.date_timer = QTimer()
        self.date_timer.timeout.connect(self.set_time)  # 刷新事件
        self.start_date_timer()
        # 实时温湿度
        self.set_temp_hum()
        self.dht22_timer = QTimer()
        self.dht22_timer.timeout.connect(self.set_temp_hum)
        self.start_hum_temp_timer()
        # 语音交互（开个后台线程）
        self.audio_thread = ac.AudioControlThread()
        self.audio_thread.start()
        # 信号槽绑定初始化
        self.slot_bind_init()

    def play_a_song(self, val):
        print('【主线程】 接收到信号了，信号值：' + val)
        # 这里要开一个线程，把val传给线程
        # 提取出歌手、歌名
        self.music_thread = ms.MusicThread(val)
        self.music_thread.start()

    ##########################################
    # 绑定信号槽
    ##########################################
    def slot_bind_init(self):
        self.audio_thread.strSignal.connect(self.play_a_song)

    ##########################################
    # 天气初始化
    ##########################################
    def __weather__init(self):
        # self.city, self.weather, self.temperature, self.wind_direction = self.get_weather()
        self.city, self.weather, self.temperature, self.wind_direction = weather.get_weather_by_GaoDe()
        self.ui.cur_temp_label.setText(self.temperature + "°C")
        self.ui.city_label.setText(self.city)
        self.ui.wea_info_label.setText(self.weather)
        self.ui.wea_wind_label.setText(self.wind_direction + "风")
    ##########################################
    # 设置当前时间
    ##########################################
    def set_time(self):
        # 格式化
        time = QDateTime.currentDateTime().toString("yyyy-MM-dd-hh:mm:ss-dddd")
        time_list = time.split('-')
        year = time_list[0]
        month = time_list[1]
        day = time_list[2]
        cur_time = time_list[3]
        cur_day = time_list[4]

        self.ui.cur_date_label.setText(year + '年' + month + '月' + day + '日')
        self.ui.cur_day_label.setText(cur_day)
        self.ui.cur_time_label.setText(cur_time)

    def start_date_timer(self):
        # 1s刷新1次
        self.date_timer.start(1000)

    ##########################################
    # 设置实时温湿度
    ##########################################
    def set_temp_hum(self):
        temperature, humidity = weather.get_temp_hum()
        self.ui.cur_env_hum_label.setText("实时湿度：" + str(round(humidity, 2)) + "%")
        self.ui.cur_env_temp_label.setText("实时温度：" + str(round(temperature, 2)) + "°C")

    def start_hum_temp_timer(self):
        self.dht22_timer.start(10000)

    ##########################################
    # 语音交互
    # 根据语音控制子线程返回的结果，进行下一步操作
    ##########################################
    def voice_interaction(self):
        pass

if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()
    sys.exit(app.exec_())